import store from '@/store'
import router from '@/router'
import NProgress from 'nprogress' //
import 'nprogress/nprogress.css' //
import getPageTitle from '@/utils/get-page-title'
NProgress.start()
// 创建白名单
const writelist = ['/login', '/404']
router.beforeEach((to, from, next) => {
  // 动态标题
  // document.title = '人力资源管理系统-' + to.meta.title
  document.title = getPageTitle(to.meta.title)
  console.log(to)
  const token = store.state.user.token
  if (token) {
    if (to.path === '/login') {
      next('/')
      NProgress.done()
    } else {
      if (!store.state.user.userinfo.userId) {
        store.dispatch('user/setuserinfo')
      }
      next()
    }
  } else {
    if (writelist.includes(to.path)) {
      next()
    } else {
      next('/login')
      NProgress.done()
    }
  }
})
router.afterEach(() => {
  NProgress.done()
})
